package com.csesteel.crm.control;

import com.csesteel.crm.domain.User;
import com.csesteel.crm.service.UserService;
import com.csesteel.crm.util.MD5Util;
import com.csesteel.crm.util.WebUtil;
import com.csesteel.crm.vo.ResponseInfo;
import com.csesteel.crm.vo.UserSessionInfor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class UserController {
    private Log log = LogFactory.getLog(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "login")
    public String login(@ModelAttribute("userSessionInfor") UserSessionInfor userSessionInfor,HttpServletRequest request, HttpServletResponse response)
    {

        RequestContext requestContext = new RequestContext(request);

        ResponseInfo responseInfo = new ResponseInfo();
        HttpSession session = request.getSession();
        if((userSessionInfor == null)|| (userSessionInfor.getUsername() == ""))
        {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("user.username.validate.null",new Object[]{requestContext.getMessage("user.username")}));
            session.setAttribute("responseInfo",responseInfo);
            return "index";
        }else if(userSessionInfor.getPasswd() == "")
        {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("user.passwd.validate.null",new Object[]{userSessionInfor.getPasswd()}));
            session.setAttribute("responseInfo",responseInfo);
            return "index";
        }
        User user = null;
        if(!"".equals(userSessionInfor.getPasswd()))
        {
            user = userService.getUserByNameAndPasswd(userSessionInfor.getUsername(), userSessionInfor.getPasswd());
        }
        log.info("username:"+user.getUsername());
        if (user != null)
        {
            responseInfo.setResult("0");
            responseInfo.setMessage(userSessionInfor.getUsername());
            session.setAttribute("responseInfo",responseInfo);
            return "success";
        } else {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("username.or.password.mistake"));
            session.setAttribute("responseInfo",responseInfo);

            return "index";
        }
    }


    @RequestMapping("getJsonValue")
    public void getJsonValue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RequestContext requestContext = new RequestContext(request);
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setResult("00000");
        responseInfo.setMessage(requestContext.getMessage("user.username.again"));
//        PrintWriter out = response.getWriter();
//        out.print(responseInfo.toString());
         WebUtil.responseToJson(response,responseInfo);
    }

}